package com.czf.testmodule.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zfChen
 * @create 2022/3/11 10:08
 */
@Service
@Slf4j
public class ParamService {

    /**
     * 通过上传的文件和前缀或后缀，得到文件中的参数和值
     * @param file
     * @param prefix
     * @param suffix
     * @return
     */
    public Map<String, String> scanFile(MultipartFile file,String prefix,String suffix){
        try {
            InputStream stream = file.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            List<String> params = new ArrayList<>();
            List<String> paramValues = new ArrayList<>();

            String line = null;
            while ((line = reader.readLine()) != null) {
                // 逐行扫描文件，得到参数和值
                String replace = line.replace(" ", "");
                if (replace.startsWith("#")) {
                    continue;
                }
                String param = getParam(line);
                String paramValue = getParamValue(line);
                if (!StringUtils.isEmpty(param) && !StringUtils.isEmpty(paramValue)) {
                    params.add(param);
                    paramValues.add(paramValue);
                }
            }
            reader.close();
            stream.close();

            // 得到字段和值
            Map<String, String> map = mergeToMap(params, paramValues,prefix,suffix);
            return map;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取文件中的字段
     * @param str
     * @return
     */
    public String getParam(String str){
        Pattern compile = Pattern.compile("\\.Set(\\w+)");
        Matcher matcher = compile.matcher(str);
        String s = null;
        while (matcher.find()){
            String group = matcher.group(1);
            s = group.substring(0,1).toLowerCase().concat(group.substring(1,group.length()));
        }
        return s;
    }

    /**
     * 获取文件中的值
     * @param str
     * @return
     */
    public String getParamValue(String str){
        Pattern compile = Pattern.compile("\\((.+)\\)");
        Matcher matcher = compile.matcher(str);
        String s = null;
        while (matcher.find()){
            s = matcher.group(1);
        }
        return s;
    }

    /**
     * 将前缀或后缀加到字段名上，然后将字段与值组合为map
     * @param params
     * @param paramValues
     * @param prefix
     * @param suffix
     * @return
     */
    public Map<String,String> mergeToMap(List<String> params,List<String> paramValues,String prefix,String suffix){
        HashMap<String, String> map = new HashMap<>();
        String repeatParam = "";
        int num = 0;
        for (int i = 0; i < params.size(); i++) {
            if (StringUtils.isEmpty(map.get(params.get(i)))) {
                map.put(prefix+params.get(i)+suffix, paramValues.get(i));
            } else {
                if (repeatParam.equals(params.get(i))) {
                    map.put(prefix+params.get(i)+suffix+"_"+(++num), paramValues.get(i));
                } else {
                    num = 0;
                    map.put(prefix+params.get(i)+suffix+"_"+(++num), paramValues.get(i));
                    repeatParam = params.get(i);
                }
            }
        }
       return map;
    }

    /**
     * 将insert语句的sql的字段和值与参数文件中的字段和值进行比较
     * @param sqlMap
     * @param paramMap
     * @return
     */
    public List<Object> compareMap(Map<Object,Object> sqlMap, Map<String,String> paramMap){
        List<Object> list = new ArrayList<>();
        for (Object paramkey:paramMap.keySet()) {
            String paramValue = paramMap.get(paramkey);
            String sqlValue = (String) sqlMap.get(paramkey);
            if (StringUtils.isEmpty(sqlValue)) {
                list.add(paramkey);
                continue;
            }
            if (!sqlValue.equals(paramValue) && !sqlValue.toLowerCase().equals(paramValue.toLowerCase())) {
                list.add(paramkey);
            }
        }
        return list;
    }



}
